package com.qys.livesMall.anchor.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qys.livesMall.anchor.model.dto.lives.sys.QueryUserGradeIntegralDTO;
import com.qys.livesMall.anchor.model.vo.lives.sys.UserGradeIntegralPageVO;
import com.qys.livesMall.anchor.service.UserGradeIntegralService;
import com.qys.livesMall.app.user.mapper.H2AppUserMapper;
import com.qys.livesMall.app.user.model.entity.H2AppUser;
import com.qys.livesMall.common.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author yxh
 */
@RestController
@Tag(name = "sys-用户直播间消费等级")
@RequestMapping("/sys/anchor/user/gradeIntegral")
public class SysUserGradeIntegralController {

    @Resource
    private UserGradeIntegralService userGradeIntegralService;
    @Resource
    private H2AppUserMapper appUserMapper;

    @PostMapping("/page")
    @Operation(summary = "用户直播间消费等级分页")
    public R<IPage<UserGradeIntegralPageVO>> page(@RequestBody QueryUserGradeIntegralDTO dto){
        return R.ok(userGradeIntegralService.getUserGradeIntegralPage(dto));
    }

    @PostMapping("/changeLiveViewPermission/{userId}")
    @Operation(summary = "变更用户观看直播权限")
    public R<?> changeLiveViewPermission(@PathVariable Long userId){
        H2AppUser oldAppUser = appUserMapper.selectById(userId);
        if(oldAppUser == null){
            return R.fail();
        }
        H2AppUser appUser = new H2AppUser();
        appUser.setId(oldAppUser.getId());
        appUser.setLiveViewPermission(0 == oldAppUser.getLiveViewPermission() ? 1 : 0);
        appUserMapper.updateById(appUser);
        return R.ok();
    }
}
